﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;
using System.Data;

namespace CoastalClimbing.Auth
{
    public partial class Members : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                this.BindInfo();
        }

        private void BindInfo()
        {
            try
            {
                grvMembers.DataSource = DAL.MemberSelect();
                grvMembers.DataBind();

                chkMembers.Checked = DAL.GearSelect("MEMBERS");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            grvMembers.EditIndex = -1;
            this.BindInfo();
        }

        protected void EditRecord(object sender, GridViewEditEventArgs e)
        {
            grvMembers.EditIndex = e.NewEditIndex;
            this.BindInfo();
        }

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            string memberID = grvMembers.DataKeys[e.RowIndex].Value.ToString();            

            GridViewRow row = grvMembers.Rows[e.RowIndex];
            TextBox txtFirstName = (TextBox)row.FindControl("txtFirstName");
            TextBox txtLastName = (TextBox)row.FindControl("txtLastName");
            TextBox txtDateJoined = (TextBox)row.FindControl("txtDateJoined");
            
            try
            {
                DAL.MemberEdit(memberID, txtFirstName.Text, txtLastName.Text, Convert.ToDateTime(txtDateJoined.Text), Membership.GetUser().ProviderUserKey.ToString());
                lblMessage.Text = "Record Updated Successfully.";
            }

            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            grvMembers.EditIndex = -1;
            this.BindInfo();
        }

        protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
        {
            string memberID = grvMembers.DataKeys[e.RowIndex].Value.ToString();

            try
            {
                DAL.MemberDelete(memberID);
                lblMessage.Text = "Record deleted Successfully.";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            grvMembers.EditIndex = -1;
            this.BindInfo();
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this member?')");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DAL.MemberCreate(txtFirstName.Text, txtLastName.Text, Convert.ToDateTime(txtDateJoined.Text), Membership.GetUser().ProviderUserKey.ToString());                        
            this.BindInfo();

            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtDateJoined.Text = "";
        }
        
        protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvMembers.PageIndex = e.NewPageIndex;  
            this.BindInfo();        
        }

        protected void chkMembers_CheckedChanged(object sender, EventArgs e)
        {
            DAL.GearChange(chkMembers.Checked, "MEMBERS");
        }
    }
}